﻿@page "/CarList"
@using ICarlifeService;
@using Microsoft.EntityFrameworkCore;
@using Models.Car_Li;
@using Commonality.DTO.Car_Li;
@using Carlife.Inf;
@inject NavigationManager NavigationManager
@inject ICarListService _carList

<PageTitle>车辆销售列表</PageTitle>
<div>
    <h3>车辆销售列表</h3>
    <Chart ChartType="ChartType.Bar" OnInitAsync="() => OnInitStack(true)" />
    <EditForm Model=@carList>
        <div class="input-group mb-3">
            <div class="input-group-prepend col-12 col-sm-6">
                <span class="input-group-text" id="inputGroup-sizing-default">车辆类型：</span>
            </div>
            <InputText @bind-Value=name class="form-control" aria-describedby="inputGroup-sizing-default"></InputText>
            <ValidationMessage For="@(() =>name)" />

            <DateTimePicker TValue="DateTime" @bind-Value="@ctime" ShowLabel="true" DisplayText="开始日期" ViewMode="DatePickerViewMode.DateTime" Format="yyyy-MM-dd" />-
            <DateTimePicker TValue="DateTime" @bind-Value="@Utime" ShowLabel="true" DisplayText="结束日期" ViewMode="DatePickerViewMode.DateTime" Format="yyyy-MM-dd" />

            <button id="search" class="btn btn-outline-secondary" @onclick="Query">查询</button>
        </div>
    </EditForm>
    <div class="col-12 col-sm-6">
        <ButtonUpload TValue="string" IsDirectory="true" OnDelete="@(fileName => Task.FromResult(true))"></ButtonUpload>
       
    </div>
  
    <table TItem="Foo" class="table-responsive" width="90%">
        <tr style="border:solid 2px black">
            <td style="border:solid 1px black">ID</td>
            <td style="border:solid 1px black;background-color:black;color:white">车辆品牌</td>
            <td style="border:solid 1px black;background-color:black;color:white">车辆系列</td>
            <td style="border:solid 1px black;background-color:black;color:white">车辆型号</td>
            <td style="border:solid 1px black;background-color:black;color:white">车牌号</td>
            <td style="border:solid 1px black;background-color:black;color:white">销量(辆)</td>
            <td style="border:solid 1px black;background-color:black;color:white">车架号</td>
            <td style="border:solid 1px black;background-color:black;color:white">发动机号</td>
            <td style="border:solid 1px black;background-color:black;color:white">起租日期</td>
            <td style="border:solid 1px black;background-color:black;color:white">操作</td>
        </tr>
        @foreach (var item in carList)
        {
            <tr style="border:solid 1px black">
                <td style="border:solid 1px black">@item.MarketId</td>
                <td style="border:solid 1px black">@item.MarketName</td>
                <td style="border:solid 1px black">@item.MarketSeries</td>
                <td style="border:solid 1px black">@item.Tname</td>
                <td style="border:solid 1px black">@item.SalesNumber</td>
                <td style="border:solid 1px black">@item.MarketNum</td>
                <td style="border:solid 1px black">@item.Marketcard</td>
                <td style="border:solid 1px black">@item.MarketEngine</td>
                <td style="border:solid 1px black">@item.MarketCreateDate</td>
                <td>
                    <a href="/AddBook?Id=@item.MarketId">编辑</a>
                    <button id="edit" class="btn btn-primary">删除</button>
                </td>
            </tr>
        }
    </table>
    <Pagination PageCount="3" OnPageLinkClick="@OnPageClicktry" />
  
</div>



@code {
    private static IEnumerable<int> PageItemsSource => new int[]
     {
        3,
        10,
        20,
        40,
        80,
        100
     };
    private List<CarSalesDTO> carList;
    private List<CarSalesFenye> fenye;
    private static CarlifeDbcontext _context;
    private List<CarMarket> cardto = new List<CarMarket>();
    public string name { get; set; }
    public int Rowcount { get; set; }

    public int Pageindex { get; set; } = 1;
    private int Pagesize = 3;


    private int Pagebefor = 1;
    [Parameter]
    public EventCallback RefterData { get; set; }

    public DateTime ctime { get; set; }
    public DateTime Utime { get; set; }


    protected override async Task OnInitializedAsync()
    {
        carList = _carList.Show().CData;
        await base.OnInitializedAsync();

    }
    public void Query()
    {
        carList = _carList.Show(name, Convert.ToString(ctime), Convert.ToString(Utime)).CData;
    }
    private Task<ChartDataSource> OnInitStack(bool stacked)
    {
        var ds = new ChartDataSource();
        ds.Options.X.Title = "车辆品牌";
        ds.Options.Y.Title = "销量";
        ds.Options.X.Stacked = stacked;
        ds.Options.Y.Stacked = stacked;
        ds.Labels = carList.Select(x => x.MarketName);
        ds.Data.Add(new ChartDataset()
                {
                    Label = $"车辆销量",
                    Data = carList.Select(x => x.MarketNum).Cast<object>()
                });

        return Task.FromResult(ds);
    }
    //分页
    private Task<QueryData<CarSalesFenye>> OnQueryAsync(QueryPageOptions options)
    {
        IEnumerable<CarSalesFenye> items = fenye;
        var total = items.Count();
        items = items.Skip((options.PageIndex - 1) * options.PageItems).Take(options.PageItems).ToList();
        return Task.FromResult(new QueryData<CarSalesFenye>() { Items = items, TotalCount = total, IsSorted = true, IsFiltered = true, IsSearch = true });
    }
    private Task OnPageClicktry(int pageIndextry)
    {
        Pageindex = pageIndextry;

        CarSalesFenye data = _carList.Show(name,null,null,Pagesize,Pageindex);

        carList = data.CData;
        StateHasChanged();
        return Task.CompletedTask;

    }
    public void DownloadLargeFileAsync()
    {
        
    }
}


